﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using Hdt.TransOcean.Dal;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormJourneyList : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormJourneyList(XafApplication application) : this() { }
        public FormJourneyList()
        {
            InitializeComponent();
        }

        private void FormJourneyList_Load(object sender, EventArgs e)
        {
            Presentation.BindGridLookupEdit(gridLookUpEditJourneyType, Presentation.GetData(EObject.JourneyType));
            BindJourneyGrid();
        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {
            this.Close();
        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {
            string code = "";

            if (gridViewJourney.FocusedRowHandle != -1)
            {
                code = gridViewJourney.GetRowCellValue(gridViewJourney.FocusedRowHandle, "code").ToString();
            
                try
                {
                    if (MessageBox.Show("Bạn muốn xóa không?", "Hành trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                    {
                        if (Dao.DeleteJourneyByCode(code) == true)
                        {
                            BindJourneyGrid();

                            MessageBox.Show("Xóa thành công", "Hành trình");
                        }
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.ToString());
                }
            }
        }

        private void BindJourneyGrid()
        {
            BindingSource bsJourney = new BindingSource();
            var journeys = Dao.SelectJourney();
            bsJourney.DataSource = journeys;
            gridControlJourney.DataSource = bsJourney;
            gridColumnCode.FieldName = "code";
            gridColumnName.FieldName = "name";
            gridColumnJourneyType.FieldName = "type";
            gridColumnDescription.FieldName = "description";
        }

        

        private void hdtManageControl1_HdtExportButtonClick()
        {
            bool flag = true;
            List<string[]> rows = new List<string[]>();

            var items = Dao.SelectJourney();

            foreach (Journey j in items)
            {
                rows.Add(new string[] { j.code.ToString(), j.name.ToString(), j.description.ToString() });
            }

            try
            {
                flag = Dao.ExportExcel(EObject.Journey, "journey.xls", rows);
            }
            catch (Exception e)
            {
                flag = false;
                MessageBox.Show(e.ToString());
            }
            finally
            {
                if (flag)
                    MessageBox.Show("Export thành công!", "Hành trình");
            }
        }

        private void hdtManageControl1_HdtImportButtonClick()
        {
            bool flag = true;
            Dao dao = new Dao();
            try
            {
                flag = Dao.ImportExcel(EObject.Journey);
            }
            catch (Exception e)
            {
                flag = false;
                MessageBox.Show(e.ToString());
            }
            finally
            {
                if (flag)
                    MessageBox.Show("Import thành công!", "Hành trình");
            }
        }

        private void hdtManageControl1_HdtNewButtonClick()
        {
            Presentation.OpenForm(MdiParent, "FormJourney", EOperation.New);
        }
        
        private void hdtManageControl1_HdtEditButtonClick()
        {
            string code = "";

            if (gridViewJourney.FocusedRowHandle != -1)
            {
                code = gridViewJourney.GetRowCellValue(gridViewJourney.FocusedRowHandle, "code").ToString();
                Presentation.OpenForm(MdiParent, "FormJourney", EOperation.Edit, code);
            }
        }

        private void hdtManageControl1_HdtSaveButtonClick()
        {

        }

        private void hdtManageControl1_HdtSearchButtonClick()
        {

        }

        private void gridViewJourney_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            Presentation.DisplayButtonByFocusedRowHandle(hdtManageControl1,e.FocusedRowHandle);
        }
        
    }
}
